﻿using BLevy.Negocio;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BLevy.UI
{
    public partial class frmCuponesReporteFiltro : Form
    {
        public frmCuponesReporteFiltro()
        {
            InitializeComponent();
        }

        private void IniciarControles()
        {
            chkCompleto.CheckState = CheckState.Checked;
            txtNroCertificado.Enabled = false;
            dtpDesde.Enabled = false;
            dtpHasta.Enabled = false;

        }


        private void frmCuponesReporteFiltro_Load(object sender, EventArgs e)
        {
            IniciarControles();
        }

        private void chkCertificado_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCertificado.CheckState == CheckState.Checked)
                txtNroCertificado.Enabled = true;
            else
                txtNroCertificado.Enabled = false;
        }

        private void chkFechas_CheckedChanged(object sender, EventArgs e)
        {
            if (chkFechas.CheckState == CheckState.Checked)
            {
                dtpHasta.Enabled = true;
                dtpDesde.Enabled = true;
            }
            else
            {
                dtpDesde.Enabled = false;
                dtpHasta.Enabled = false;
            }
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            string numero_certificado = txtNroCertificado.Text == string.Empty ? null : txtNroCertificado.Text;

            DateTime? desde = chkFechas.Checked ? (DateTime?)dtpDesde.Value : null;
            DateTime? hasta = chkFechas.Checked ? (DateTime?)dtpHasta.Value : null;

            try
            {
                var query = (from row in Cupones.FindAllCondicionalReporte(null, numero_certificado, desde, hasta).OrderByDescending(x => x.fecha_emision)
                             select row).ToList();

                frmCuponesReporte frm = new frmCuponesReporte(query);
                frm.MdiParent = this.MdiParent;
                frm.Show();

                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
